﻿#include "PugiXmlDemo.h"
#include "pugixml/pugixml.hpp"

#include <QDir>
#include <QtDebug>
#include <QLayout>
#include <QString>
#include <QHBoxLayout>
#include <QPushButton>
#include <QFileDialog>

struct PugiXmlDemoPrivate
{
    PugiXmlDemoPrivate()
    {
    }
private:

};

#define FromUnicode(String){        \
    QString::fromLocal8Bit(String)  \
}
PugiXmlDemo::PugiXmlDemo(QWidget *parent)
    : QWidget(parent),_p(new PugiXmlDemoPrivate)
{
    setFixedSize(150,150);
    setWindowTitle(FromUnicode("Xml实例"));
    QHBoxLayout* pBoxLayout = new QHBoxLayout(this);
    QPushButton* pSelectBtn = new QPushButton(FromUnicode("选择文件"),this);
    pBoxLayout->addWidget(pSelectBtn);
    setLayout(pBoxLayout);
    connect(pSelectBtn,&QPushButton::clicked,[=]()
    {
       QString currentPath = QDir::currentPath();
       QString fileName = QFileDialog::getOpenFileName(this,
              tr("Open Xml"), currentPath, tr("Xml Files (*.xml)"));
       initFile(fileName);
    });
}

PugiXmlDemo::~PugiXmlDemo()
{

}

void PugiXmlDemo::initFile(const QString& fileName)
{
    pugi::xml_document doc;
    pugi::xml_parse_status readStatus = doc.load_file(fileName.toLocal8Bit().toStdString().c_str()
            ,pugi::parse_default,pugi::encoding_auto).status;

    if( readStatus == pugi::status_ok )
    {
        pugi::xpath_node_set root = doc.select_nodes("root/IPinfo");
        for (pugi::xpath_node_set::const_iterator itor = root.begin();itor!=root.end();++itor)
        {
            QString item = itor->node().attribute("ip").value();
            qDebug()<<item<<endl;
        }
    }
}
